package com.dataart.productshop.somepackageforspringstopfuckmybrain;

import com.dataart.productshop.Constants;
import com.dataart.productshop.file.FileInfoWrapper;
import com.dataart.productshop.file.utils.PictureUtils;
import com.dataart.productshop.model.FileInfo;
import com.dataart.productshop.service.FileInfoManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.text.SimpleDateFormat;

@Controller
public class FilePageController {
	
	@Autowired
	FileInfoManager fileInfoManager;
	
	@RequestMapping(value = "/file/{fileId}", method = RequestMethod.GET)
	public String getFileHelloPage(@PathVariable("fileId")String fileId, Model model){
		Long id = Long.parseLong(fileId);
		
		FileInfo currentFile = fileInfoManager.getByIdEager(id);
		FileInfoWrapper fileInfoWrap = new FileInfoWrapper();
		fileInfoWrap.setName(currentFile.getName());
		fileInfoWrap.setDescription(currentFile.getDescription());
		fileInfoWrap.setDownloads(currentFile.getDownloads().toString());
		fileInfoWrap.setZipFilePath(Constants.PREFIX + "/" + currentFile.getFilePath());
		fileInfoWrap.setPictureFilePathes(PictureUtils.getPathes(currentFile,Constants.PREFIX));
		fileInfoWrap.setCreatedDate(new SimpleDateFormat("MM/dd/yyyy HH:mm")
				.format(currentFile.getCreatedDate()));
		fileInfoWrap.setType(currentFile.getType().getName());
		
		model.addAttribute("fileInfo", fileInfoWrap);
		return "home";
	}
	
	
}
